package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.console.service.facade.CaStrategyCfgService;
import com.fdb.efp.console.service.vo.CaStrategyCfgVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.DateTool;
import com.fdb.basic.framework.core.util.UUIDUtil;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class CaStrategyCfgController extends AbstractController  {
	
	@Autowired
	@Qualifier("caStrategyCfgService")
	private CaStrategyCfgService caStrategyCfgService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 鍒楄〃鏁版嵁鏌ヨ
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/ca/strategy/cfgs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CaStrategyCfgVO>> queryCaStrategyCfgAll(
			@RequestBody CaStrategyCfgVO vo) {	
		// 根据用户角色获取用户权限方案
		List<CaStrategyCfgVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = caStrategyCfgService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = caStrategyCfgService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = caStrategyCfgService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = caStrategyCfgService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
		
	}
	
	/**
	 * 鏍规嵁涓婚敭鏌ヨ璇︽儏
	 * @return
	 */
	@RequestMapping(value="/ca/strategy/cfg/{id}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<CaStrategyCfgVO> queryByPk(@PathVariable("id") String id) {		
		CaStrategyCfgVO inVo = new CaStrategyCfgVO();
				inVo.setId(id);
				setUserInfoToVO(inVo);
		CaStrategyCfgVO outputVo = caStrategyCfgService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 鏍规嵁涓婚敭鍒犻櫎淇℃伅
	 * @param caStrategyCfg
	 * @return
	 */
	@RequestMapping(value="/ca/strategy/cfg", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CaStrategyCfgVO inCaStrategyCfgVo) {	
		setUserInfoToVO(inCaStrategyCfgVo);
		int outputVo = caStrategyCfgService.deleteByPk(inCaStrategyCfgVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 鏍规嵁涓婚敭鏇存柊淇℃伅
	 * @param inCaStrategyCfgVo
	 * @return
	 */
	@RequestMapping(value="/ca/strategy/cfg", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CaStrategyCfgVO inCaStrategyCfgVo) {		
		setUserInfoToVO(inCaStrategyCfgVo);
		 
		inCaStrategyCfgVo.setLastUpdateTime(DateTool.getCurrentDateTime());
		inCaStrategyCfgVo.setLastUpdateUser(inCaStrategyCfgVo.getLoginUserId());
		int outputVo = caStrategyCfgService.updateByPk(inCaStrategyCfgVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 鏂板鏁版嵁
	 * @param inCaStrategyCfgVo
	 * @return
	 */
	@RequestMapping(value="/ca/strategy/cfg", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCaStrategyCfg(@RequestBody CaStrategyCfgVO inCaStrategyCfgVo) {
		/**
		 * 【逻辑修改】
		 * List list =caStrategyCfgService.checkStateQuery(inSOrgLimitVo);
		if(!list.isEmpty())
		{
			ResponseData<Integer> responseData = new ResponseData<>();
			
			return responseData.fail( "鏂板鐨勬満鏋勫湪璇ユ椂闂存宸茬粡瀛樺湪浜嗚褰曪紝璇峰厛鍒犻櫎鍐嶆柊澧�");
		}*/
		
		setUserInfoToVO(inCaStrategyCfgVo);
		inCaStrategyCfgVo.setId(UUIDUtil.getUUID());
		inCaStrategyCfgVo.setCreateTime(DateTool.getCurrentDateTime());
		inCaStrategyCfgVo.setCreateUser(inCaStrategyCfgVo.getLoginUserId());
		inCaStrategyCfgVo.setOrgCode(inCaStrategyCfgVo.getLegalOrgCode());
		int outputVo = caStrategyCfgService.insertCaStrategyCfg(inCaStrategyCfgVo);
		return getResponseData(outputVo);
		
	}
}
